package com.sinosoft.cloud.clints;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 描述:
 *
 * @author 张旭东
 * @date 2018/11/30
 */
@Component
public class RestClient {
    private RestTemplate restTemplate;
    Log log = LogFactory.getLog(RestClient.class);
    public RestClient() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(20000);
        requestFactory.setReadTimeout(20000);
        restTemplate = new RestTemplate(requestFactory);
    }

    public String post(String url, String req) {
        String res = null;
        try {
            res = this.restTemplate.postForObject(url, req, String.class);
        } catch (Exception e) {
            return null;
        }
        return res;
    }
    public String get(String url, String req) {
        String res = null;
        try {
            res = this.restTemplate.getForObject(url,String.class);
        } catch (Exception e) {
            return null;
        }
        return res;
    }
}